home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Java Primer Plus
/
Java Primer Plus (Waite Group Proess)(1996).iso
/
chapter15
/
ServerThread.java
< prev
next >
Wrap
Text File
|
1995-12-31
|
1KB
|
84 lines
import java.net.Socket;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.awt.*;
public class ServerThread extends Thread {
static Point All[] = new Point[10];
static {
for (int j=0;j<10;++j)
All[j] = new Point(0,0);
}
int myall;
Socket mySocket;
DataInputStream datain;
DataOutputStream dataout;
public ServerThread(Socket m,int me) throws IOException {
int g;
mySocket = m;
myall = me;
datain = new DataInputStream(new BufferedInputStream(mySocket.getInputStream()));
dataout = new DataOutputStream(new BufferedOutputStream(mySocket.getOutputStream()));
}
public void run() {
int g;
try {
while (talk())
yield();
mySocket.close();
} catch (IOException E);
synchronized(All[myall]) All[myall].move(0,0);
}
private boolean talk() throws IOException {
int g;
int x,y;
x = datain.read();
y = datain.read();
if (x == -1) return false;
synchronized(All[myall])
if ((x != 0) || (y != 0)) All[myall].move(x,y);
for (g=0;g<10;++g) {
synchronized(All[g]) {
if ((All[g].x != 0) || (All[g].y != 0)) {
dataout.write(All[g].x);
dataout.write(All[g].y);
}
}
}
dataout.write(-1);
dataout.write(-1);
dataout.flush();
return true;
}
}